<template>
  <div>
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>咨询信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>学员咨询管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用户列表卡片区 -->
    <el-card class="box-card">
      <el-form :inline="true" ref="form" :model="queryMap" label-width="100px" size="small">
        <el-form-item label="咨询者姓名">
          <el-input
              @keyup.enter.native="searchConsultant"
              @clear="searchConsultant"
              clearable
              v-model="queryMap.consultantName"
              placeholder="请输入咨询者姓名查询"
          ></el-input>
        </el-form-item>

        <el-form-item label="联系方式">
          <el-input clearable @clear="searchConsultant" v-model="queryMap.phone" placeholder="请输入联系方式"></el-input>
        </el-form-item>
        <el-form-item label="咨询师">
          <el-input clearable @clear="searchConsultant" v-model="queryMap.careerPlanner" placeholder="请输入咨询师"></el-input>
        </el-form-item>
        <el-form-item label="是否报名">
          <el-select
              clearable
              v-model="queryMap.isSignup"
              @clear="searchConsultant"
              placeholder="请选择状态"
          >
            <el-option label="全部" value=""></el-option>
            <el-option label="已报名" value="1"></el-option>
            <el-option label="未报名" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left:50px;">
          <el-button  @click="reset" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="searchConsultant" icon="el-icon-search">查询</el-button>
          <el-button
              type="success"
              icon="el-icon-plus"
              @click="addDialogVisible=true"
              v-hasPermission="'consult:add'"
          >添加</el-button>
          <el-button @click="downExcel" v-hasPermission="'consult:export'"  icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table v-loading="loading" size="small" :data="consultantList" border style="width: 100%;" height="420">
        <!-- <el-table-column type="selection" width="40"></el-table-column> -->
        <el-table-column label="#" prop="consultantId" width="50"></el-table-column>
        <el-table-column prop="consultantName" label="咨询者姓名" width="100"></el-table-column>


        <el-table-column prop="sex"  label="性别" width="60">
        <template slot-scope="scope">
          <span>{{showSex(scope.row)}}</span>
        </template>
        </el-table-column>
        <el-table-column prop="brithday" label="出生日期" width="100" sortable></el-table-column>
        <el-table-column prop="phone" label="联系方式" width="120" sortable></el-table-column>
        <el-table-column prop="qqNumber" label="QQ号码" width="120"></el-table-column>
        <el-table-column prop="wechatNumber" label="微信账号" width="120"></el-table-column>
        <el-table-column prop="isSignup" label="是否报名" width="80">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.isSignup"
                :active-value="2"
                :inactive-value="1"
                @change="changConsultStatus(scope.row)">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="chooseMajor"  label="选择专业" width="100">
          <template slot-scope="scope">
            <span>{{showMajorName(scope.row)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button   v-hasPermission="'user:edit'" size="small" type="primary" icon="el-icon-edit-outline" @click="edit(scope.row.consultantId)"></el-button>
            <el-button v-hasPermission="'user:delete'" type="danger" size="small" icon="el-icon-delete" @click="del(scope.row.consultantId)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
          style="margin-top:10px;"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryMap.pageNo"
          :page-sizes="[6, 10, 20, 30]"
          :page-size="queryMap.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>

      <!-- 添加对话框 -->
      <el-dialog title="添加学员信息" @close="closeDialog" :visible.sync="addDialogVisible" width="50%;">
        <!-- 表单 -->
        <span>
          <el-form
              :model="addForm"
              :label-position="labelPosition"
              :rules="addFormRules"
              ref="addFormRef"
              label-width="80px"
          >
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="咨询者名" prop="consultantName">
                    <el-input v-model="addForm.consultantName"></el-input>
                  </el-form-item>
                </div>
              </el-col>
               <el-col :span="12">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="addForm.sex">
                      <el-radio label="1" :value="1">男</el-radio>
                      <el-radio label="0" :value="0">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="联系电话" prop="phone">
                    <el-input v-model="addForm.phone"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
               <div class="grid-content bg-purple">
                  <el-form-item label="咨询师" prop="careerPlanner">
                    <el-input v-model="addForm.careerPlanner"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="QQ号码" prop="qqNumber">
                    <el-input v-model="addForm.qqNumber"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
               <div class="grid-content bg-purple">
                  <el-form-item label="微信账号" prop="wechatNumber">
                    <el-input v-model="addForm.wechatNumber"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
             <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="家长电话" prop="parentMoblie">
                    <el-input v-model="addForm.parentMoblie"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
               <div class="grid-content bg-purple">

                 <el-form-item label="选择专业" prop="chooseMajor">
                    <el-select v-model="addForm.chooseMajor" placeholder="请选择专业">
                      <el-option label="软件开发" value="1"></el-option>
                      <el-option label="UI设计" value="2"></el-option>
                      <el-option label="网络安全" value="3"></el-option>
                      <el-option label="人工智能" value="4"></el-option>
                      <el-option label="大数据" value="5"></el-option>
                      <el-option label="视频剪辑" value="6"></el-option>
                      <el-option label="其他" value="7"></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-form-item prop="brithday" label="出生日期">
              <el-col :span="12">
                <el-date-picker
                    type="date"
                    value-format="yyyy年MM月dd日"
                    placeholder="选择出生日期"
                    v-model="addForm.brithday"
                    style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
             <el-form-item label="目前状况" prop="currentSituation">
              <el-input v-model="addForm.currentSituation"></el-input>
            </el-form-item>
             <el-form-item label="教育程度" prop="enrolEducation">
              <el-input v-model="addForm.enrolEducation"></el-input>
            </el-form-item>

             <el-form-item label="家庭住址" prop="address">
              <el-input v-model="addForm.address"></el-input>
            </el-form-item>
            <el-form-item label="毕业学校" prop="graduateSchool">
              <el-input v-model="addForm.graduateSchool"></el-input>
            </el-form-item>

             <el-form-item label="备注" prop="remakes">
              <el-input type="textarea" v-model="addForm.remakes"></el-input>
            </el-form-item>


          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button
              type="primary"
              @click="addConsultant"
              :loading="btnLoading"
              :disabled="btnDisabled"
          >确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editClose">
        <span>
          <el-form
              :model="editForm"
              :label-position="labelPosition"
              :rules="addFormRules"
              ref="editFormRef"
              label-width="80px"
          >
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="咨询者名" prop="consultantName">
                    <el-input v-model="editForm.consultantName" :disabled="true"></el-input>
                    <el-input
                        type="hidden"
                        v-model="editForm.consultantId"
                        :disabled="true"
                        style="display:none;"
                    ></el-input>
                  </el-form-item>
                </div>
              </el-col>
               <el-col :span="12">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="editForm.sex">
                      <el-radio label="1" :value="1">男</el-radio>
                      <el-radio label="0" :value="0">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="联系电话" prop="phone">
                    <el-input v-model="editForm.phone"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple-light">
                   <el-form-item label="咨询师" prop="careerPlanner">
                      <el-input v-model="editForm.careerPlanner"></el-input>
                   </el-form-item>
                </div>
              </el-col>
            </el-row>

             <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="QQ号码" prop="qqNumber">
                    <el-input v-model="editForm.qqNumber"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
               <div class="grid-content bg-purple">
                  <el-form-item label="微信账号" prop="wechatNumber">
                    <el-input v-model="editForm.wechatNumber"></el-input>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="家长电话" prop="parentMoblie">
                    <el-input v-model="editForm.parentMoblie"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
               <div class="grid-content bg-purple">

                 <el-form-item label="选择专业" prop="chooseMajor">
                    <el-select v-model="editForm.chooseMajor" placeholder="请选择专业">
                      <el-option label="软件开发" :value="1"></el-option>
                      <el-option label="UI设计" :value="2"></el-option>
                      <el-option label="网络安全" :value="3"></el-option>
                      <el-option label="人工智能" :value="4"></el-option>
                      <el-option label="大数据" :value="5"></el-option>
                      <el-option label="视频剪辑" :value="6"></el-option>
                      <el-option label="其他" :value="7"></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-form-item prop="brithday" label="出生日期">
              <el-col :span="11">
                <el-date-picker
                    type="date"
                    value-format="yyyy年MM月dd日"
                    placeholder="选择出生日期"
                    v-model="editForm.brithday"
                    style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>

             <el-form-item label="目前状况" prop="currentSituation">
              <el-input v-model="editForm.currentSituation"></el-input>
            </el-form-item>
             <el-form-item label="教育程度" prop="enrolEducation">
              <el-input v-model="editForm.enrolEducation"></el-input>
            </el-form-item>
             <el-form-item label="家庭住址" prop="address">
              <el-input v-model="editForm.address"></el-input>
            </el-form-item>
            <el-form-item label="毕业学校" prop="graduateSchool">
              <el-input v-model="editForm.graduateSchool"></el-input>
            </el-form-item>
              <el-form-item label="备注" prop="remakes">
              <el-input type="textarea" v-model="editForm.remakes"></el-input>
            </el-form-item>
          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button
              type="primary"
              @click="updateConsultant"
              :loading="btnLoading"
              :disabled="btnDisabled"
          >确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    const checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[34578]\d{9}$$/;
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (phoneReg.test(value)) {
            callback();
          } else {
            callback(new Error("电话号码格式不正确"));
          }
        }
      }, 100);
    };
    return {
      btnLoading: false,
      btnDisabled: false,
      departments: [],
      loading: true,
      total: 0,
      addDialogVisible: false, //添加对话框,
      editDialogVisible: false, //修改对话框
      assignDialogVisible: false, //分配角色对话框
      labelPosition: "right", //lable对齐方式
      //查询对象
      queryMap: {
        pageNum: 1,
        pageSize: 6,
        consultantName: "",
        careerPlanner: "",
        phone: "",
        isSignup:""
      },
      consultantList: [],

      addForm: {
        consultantName: "",
        sex: "",
        brithday: "",
        phone: "",
        qqNumber: "",
        wechatNumber: "",
        currentSituation: "",
        enrolEducation: "",
        parentMoblie: "",
        chooseMajor: "",
        address: "",
        graduateSchool: "",
        careerPlanner: "",
        isSignup: "",
        remakes: ""
      }, //添加表单
      editForm: {}, //更新表单
      addFormRules: {
        consultantName: [
          { required: true, message: "请输入咨询者名字", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        phone: [
          {
            required: true,
            message: "请输入联系方式",
            validator: checkPhone,
            trigger: "blur"
          }
        ],
        careerPlanner: [
          { required: true, message: "请输入咨询师", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {

    /**
     * 重置
     */
    reset(){
      this.queryMap= {
        pageNum: 1,
        pageSize: 6,
        consultantName: "",
        careerPlanner: "",
        phone: "",
        isSignup:""
      };
    },
    /**
     * 加载学员信息表格
     */
    downExcel() {
      const $this = this;
      const res = axios
          .request({
            url: "http://localhost:8989/consult/manage/excel",
            method: "post",
            responseType: "blob"
          })
          .then(res => {
            if (res.headers["content-type"] === "application/json") {
              return $this.$message.error(
                  "Subject does not have permission [user:export]"
              );
            }
            const data = res.data;
            let url = window.URL.createObjectURL(data); // 将二进制文件转化为可访问的url
            const a = document.createElement("a");
            document.body.appendChild(a);
            a.href = url;
            a.download = "咨询信息列表.xls";
            a.click();
            window.URL.revokeObjectURL(url);
          });
    },

    /**
     * 加载学员列表
     */
    async getConsultantList() {
      const { data: res } = await this.$http.get("consult/findList", {
        params: this.queryMap
      });
      if(!res.success){
        return this.$message.error("获取学员列表失败:"+res.data.errorMsg);
      }
      this.total = res.data.total;
      this.consultantList = res.data.rows;
    },

    /**
     * 删除学员
     */
    async del(id) {
      const res = await this.$confirm(
          "此操作将永久删除该学员, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
      if (res === "confirm") {
        const { data: res } = await this.$http.delete("consult/delete/" + id);
        console.log(res);
        if(res.success){
          this.$notify.success({
            title:'操作成功',
            message:'学员删除成功',
          });
          await this.getConsultantList();
        }else {
          this.$message.error(res.data.errorMsg);
        }
      }
    },
    /**
     * 添加学员信息
     */
    async addConsultant() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return;
        } else {
          this.btnLoading = true;
          this.btnDisabled = true;
          const { data: res } = await this.$http.post("consult/add", this.addForm);
          if(res.success){
            this.$notify.success({
              title:'操作成功',
              message:'学员添加成功',
            });
            this.addForm = {};
            await this.getConsultantList();
          } else {
            return this.$message.error("学员添加失败:" + res.data.errorMsg);
          }
          this.addDialogVisible = false;
          this.btnLoading = false;
          this.btnDisabled = false;
        }
      });
    },
    /**
     * 更新学员信息
     */
    async updateConsultant() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return;
        } else {
          this.btnLoading = true;
          this.btnDisabled = true;
          const { data: res } = await this.$http.put(
              "consult/update/" + this.editForm.consultantId,
              this.editForm
          );
          if(res.success){
            this.$notify({
              title: "操作成功",
              message: "学员基本信息已更新",
              type: "success"
            });
            this.editForm = {};
            await this.getConsultantList();
          } else {
            this.$message.error("学员信息更新失败:" + res.data.errorMsg);
          }
          this.editDialogVisible = false;
          this.btnLoading = false;
          this.btnDisabled = false;
        }
      });
    },


    /**
     * 搜索学员
     */
    searchConsultant() {
      this.queryMap.pageNum = 1;
      this.getConsultantList();
    },
    /**
     * 修改学员信息
     */
    async edit(id) {
      const { data: res } = await this.$http.get("consult/edit/" + id);
      if(res.success){
        this.editForm = res.data;
        this.editDialogVisible = true;
      } else {
        return this.$message.error("学员信息编辑失败:" + res.data.errorMsg);
      }
    },
    /**
     *  改变页码
     */
    handleSizeChange(newSize) {
      this.queryMap.pageSize = newSize;
      this.getConsultantList();
    },
    /**
     * 翻页
     */
    handleCurrentChange(current) {
      this.queryMap.pageNum = current;
      this.getConsultantList();
    },

    /**
     * 关闭添加弹出框
     */
    closeDialog() {
      this.$refs.addFormRef.clearValidate();
      this.addForm.birth = "";
      this.addForm = {};
    },
    /**
     * 关闭编辑弹出框
     */
    editClose() {
      this.$refs.editFormRef.clearValidate();
      this.editForm = {};
    },
    /**
     * 设为已报名
     */
    async changConsultStatus(row) {
      let sta = 2;
      if(row.status){
        sta = 1;
      }else{
        sta=2;
      }
      const { data: res } = await this.$http.put(
          "consult/updateStatus/" + row.consultantId + "/" + sta
      );
      if(!res.success){
        this.$message.error("更新用户状态失败:" + res.data.errorMsg);
        row.status = !row.status;
      } else {
        const message = (sta==1) ? '已报名' : '未报名';
        this.$notify.success({
          title: '操作成功',
          message: message,
        });
      }
    },





    /**
     * 显示用户性别
     */
    showSex(row, column) {
      let sex='';
      if(row.sex==1){
        sex = '男';
      }else {
        sex = '女';
      }
      return sex;
    },

    /**
     * 显示专业名称
     */
    showMajorName(row, column) {
      let majorName = '';
      if(row.chooseMajor===1){
        majorName ='软件开发';
      }else if(row.chooseMajor===2){
        majorName ='UI设计';
      }else if(row.chooseMajor===3){
        majorName ='网络安全';
      }else if(row.chooseMajor===4){
        majorName ='人工智能';
      }else if(row.chooseMajor===5){
        majorName ='大数据';
      }else if(row.chooseMajor===6){
        majorName ='视频剪辑';
      }else if(row.chooseMajor===7){
        majorName ='其他';
      }
      return majorName;
    },
  },
  created() {
    this.getConsultantList();
    setTimeout(() => {
      this.loading = false;
    }, 500);
  }
};
</script>
